using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIWorldPanel : MonoBehaviour
{
	[SerializeField]
	private Transform worldsGridTransform;

	[SerializeField]
	private UIWorldLabel label;

	[SerializeField]
	private UILevelPanel levelPanelPrefab;

	[SerializeField]
	private UILockedLevelPanel lockedLevelPanelPrefab;

	private int worldId;

	private List<ILevelProgress> levelsProgress = new List<ILevelProgress>();

	private List<GameObject> levelsPanels = new List<GameObject>();

	private RectTransform parentRectTransform;

	private RectTransform thisRectTransform;

	private float anchoredY;

	private float panelEnd;

	private float multipledHeight;

	private bool initialized;

	public int WorldId
	{
		get
		{
			return worldId;
		}
		protected set
		{
			worldId = value;
		}
	}

	private void Awake()
	{
		parentRectTransform = base.transform.parent.GetComponent<RectTransform>();
		thisRectTransform = GetComponent<RectTransform>();
	}

	public void Initialize(int worldId, List<ILevelProgress> levelsProgress)
	{
		if (levelsPanels.Count != 0)
		{
			DebugUtil.Log("Panel for world: " + worldId + " already created, Use recreate to set new values");
			return;
		}
		WorldId = worldId;
		this.levelsProgress = levelsProgress;
		CreateLevelsPanels();
		InitializeWorldLabel();
		StartCoroutine(TurnOffContentSizeFitter());
	}

	public void RecreatePanels(List<ILevelProgress> levelsProgress)
	{
		this.levelsProgress = levelsProgress;
		DestroyOldLevels();
		CreateLevelsPanels();
		InitializeWorldLabel();
	}

	private void DestroyOldLevels()
	{
		for (int i = 0; i < levelsPanels.Count; i++)
		{
			UnityEngine.Object.Destroy(levelsPanels[i]);
		}
		levelsPanels.Clear();
	}

	private IEnumerator TurnOffContentSizeFitter()
	{
		yield return new WaitForEndOfFrame();
		thisRectTransform.sizeDelta = worldsGridTransform.GetComponent<RectTransform>().sizeDelta;
		worldsGridTransform.GetComponent<ContentSizeFitter>().enabled = false;
		Object.FindObjectOfType<UILevelsPicker>().OnWorldInitialized();
	}

	public void OnPanelInVerticalLayout()
	{
		Vector2 anchoredPosition = thisRectTransform.anchoredPosition;
		anchoredY = Mathf.Abs(anchoredPosition.y);
		Vector2 sizeDelta = thisRectTransform.sizeDelta;
		float y = sizeDelta.y;
		multipledHeight = 1.1f * y;
		panelEnd = anchoredY + y + 100f;
		initialized = true;
		label.EnableLabelMovement();
	}

	private void InitializeWorldLabel()
	{
		int num = 0;
		int num2 = 0;
		foreach (ILevelProgress item in levelsProgress)
		{
			num += item.Stars;
			if (item.HaveCrown)
			{
				num2++;
			}
		}
		label.Initialize(WorldId, num, levelsProgress.Count * 3, num2, levelsProgress.Count);
	}

	private void CreateLevelsPanels()
	{
		for (int i = 0; i < levelsProgress.Count; i++)
		{
			ILevelProgress levelProgress = levelsProgress[i];
			if (!levelProgress.IsLocked)
			{
				UILevelPanel uILevelPanel = UnityEngine.Object.Instantiate(levelPanelPrefab, worldsGridTransform);
				uILevelPanel.Initialize(WorldId, levelProgress.WorldLevelId, levelProgress.Stars, levelProgress.HaveCrown, levelProgress.AbsoluteLevelId);
				levelsPanels.Add(uILevelPanel.gameObject);
			}
			else
			{
				UILockedLevelPanel uILockedLevelPanel = UnityEngine.Object.Instantiate(lockedLevelPanelPrefab, worldsGridTransform);
				levelsPanels.Add(uILockedLevelPanel.gameObject);
			}
		}
	}

	private void Update()
	{
		if (!initialized)
		{
			return;
		}
		Vector2 anchoredPosition = parentRectTransform.anchoredPosition;
		if (!(anchoredPosition.y > panelEnd))
		{
			Vector2 anchoredPosition2 = parentRectTransform.anchoredPosition;
			if (!(anchoredPosition2.y + multipledHeight < anchoredY))
			{
				if (!worldsGridTransform.gameObject.activeInHierarchy)
				{
					worldsGridTransform.gameObject.SetActive(value: true);
					label.gameObject.SetActive(value: true);
				}
				return;
			}
		}
		if (worldsGridTransform.gameObject.activeInHierarchy)
		{
			worldsGridTransform.gameObject.SetActive(value: false);
			label.gameObject.SetActive(value: false);
		}
	}
}
